/*
* Authoer:JacksonCoder
* CreateTime:
* Version: 1.0.0
*/

enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter(u8), // 带数据
}

struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let number = 5;

    if number > 0 {
        println!("数字是正数");
    }
    println!("==== main ====");

    let n = -3;
    if n > 0 {
        println!("数字是正数");
    } else {
        println!("数字是非正数");
    }

    let scores = 60;

    if scores > 90 {
        println!("优秀");
    } else if scores >= 80 {
        println!("良好");
    } else if scores >= 60 {
        println!("及格");
    } else {
        println!("不及格");
    }

    let a = 1;

    let description = if a % 2 == 0 { "偶数" } else { "奇数" };
    println!("数字: {}, 是 {}", a, description);

    let p = 3;

    match p {
        1 => println!("一"),
        2 => println!("二"),
        3 => println!("三"),
        4 => println!("四"),
        _ => println!("其他"),
    }

    let day = match_return_value(6);
    println!("今天是 {}", day);

    // 模式匹配
    let score = 85;

    match score {
        90..100 => println!("优秀"),
        80..89 => println!("良好"),
        60..79 => println!("及格"),
        _ => println!("不及格"),
    }

    let coin = Coin::Quarter(50);
    println!("价值 {}美分", value_in_cents(coin));

    // 结构体解构
    let p = Point { x: 1, y: 3 };
    match p {
        Point { x, y: 0 } => println!("在 x 轴:{}", x),
        Point { x: 0, y } => println!("在 y 轴:{}", y),
        Point { x, y } => println!("在 ({},{})", x, y),
    }

    let num = Some(5);

    match num {
        Some(x) if x % 2 == 0 => println!("偶数: {}", x),
        Some(x) => println!("奇数: {}", x),
        None => println!("无值"),
    }

    // 绑定与@匹配
    let msg = "hello";
    match msg {
        x @ "hello" => println!("匹配: {}", x),
        _ => println!("其他"),
    }

    // 多模式与or
    let x = 1;
    match x {
        1 | 2 => println!("一或二"),
        3..=5 => println!("三到五"),
        _ => println!("其他"),
    }

    // if let 简化 （match简化版）
    let some_value = Some(3);
    if let Some(x) = some_value {
        println!("值: {} ", x);
    } else {
        print!("无值\n");
    }


}

fn value_in_cents(coin: Coin) -> u8 {
    match coin {
        Coin::Penny => 1,
        Coin::Nickel => 5,
        Coin::Dime => 10,
        Coin::Quarter(state) => {
            println!("州代码: {}", state);
            25
        }
    }
}

fn match_return_value(day: i32) -> &'static str {
    match day {
        1 => "星期一",
        2 => "星期二",
        3 => "星期三",
        4 => "星期四",
        5 => "星期五",
        6 => "星期六",
        7 => "星期七",
        _ => "你可能不是地球人",
    }
}
